//
//  ViewController.m
//  CoreData_42
//
//  Created by dllo on 15/11/20.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "ViewController.h"
#import "CoreDataManager.h"
#import "Student.h"

@interface ViewController ()

@property (nonatomic, strong) CoreDataManager *manager;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.manager = [CoreDataManager defaults];
    
    NSLog(@"url ====== %@", [self.manager applicationDocumentsDirectory]);
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(70, 100, 100, 30);
    [button setTitle:@"add" forState:UIControlStateNormal];
    [self.view addSubview:button];
    [button addTarget:self action:@selector(add:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeSystem];
    button2.frame = CGRectMake(70, 200, 100, 30);
    [button2 setTitle:@"select" forState:UIControlStateNormal];
    [self.view addSubview:button2];
    [button2 addTarget:self action:@selector(selectAction:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *button3 = [UIButton buttonWithType:UIButtonTypeSystem];
    button3.frame = CGRectMake(70, 300, 100, 30);
    [button3 setTitle:@"update" forState:UIControlStateNormal];
    [self.view addSubview:button3];
    [button3 addTarget:self action:@selector(updateAction:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *button4 = [UIButton buttonWithType:UIButtonTypeSystem];
    button4.frame = CGRectMake(70, 400, 100, 30);
    [button4 setTitle:@"delete" forState:UIControlStateNormal];
    [self.view addSubview:button4];
    [button4 addTarget:self action:@selector(deleteAction:) forControlEvents:UIControlEventTouchUpInside];
}

// CoreData方法
- (void)add:(UIButton *)button {
    // 创建实体描述
    // 参数1:描述的实体
    // 参数2:数据管理器
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Student" inManagedObjectContext:self.manager.managedObjectContext];
    
    // 创建实体
    // 参数1:实体描述
    // 参数2:数据管理器
    Student *stu = [[Student alloc] initWithEntity:entity insertIntoManagedObjectContext:self.manager.managedObjectContext];
    
    stu.name = @"成精";
    stu.age = @100;
    stu.sex = @"?";
    stu.number = @10;
    stu.hobby = @"1111";
    
    [self.manager saveContext];
}

- (void)selectAction:(UIButton *)button {
    
    // 获取查询数据的请求, 相当于数据库的查询语句
    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Student"];
    
    // 生成约束 谓词
    NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"%@ == %@", @"name", @"%@"], @"成精"];
    
    // 调价约束
    request.predicate = predicate;
    
    // 通过某个key给数组排序
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
    
    [request setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
    
    NSError *error;
    
    NSArray *array = [self.manager.managedObjectContext executeFetchRequest:request error:&error];
    
    NSLog(@"%ld", array.count);
}

- (void)updateAction:(UIButton *)button {
    // 获取查询数据的请求, 相当于数据库的查询语句
    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Student"];
    
    // 生成约束 谓词
    NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"%@ == %@", @"name", @"%@"], @"成精"];
    
    // 调价约束
    request.predicate = predicate;
    
    // 通过某个key给数组排序
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
    
    [request setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
    
    NSError *error;
    
    Student *student = [[self.manager.managedObjectContext executeFetchRequest:request error:&error] lastObject];
    student.sex = @"大约摸是女的";
    
    [self.manager saveContext];
    
    
}

- (void)deleteAction:(UIButton *)button {
    // 获取查询数据的请求, 相当于数据库的查询语句
    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Student"];
    
    // 生成约束 谓词
    NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"%@ == %@", @"sex", @"%@"], @"?"];
    
    // 调价约束
    request.predicate = predicate;
    
    // 通过某个key给数组排序
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"sex" ascending:YES];
    
    [request setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
    
    NSError *error;
    
    NSArray *array = [self.manager.managedObjectContext executeFetchRequest:request error:&error];

    
    for (Student *tempStu in array) {
        if ([tempStu.sex isEqualToString:@"?"]) {
            [self.manager.managedObjectContext deleteObject:tempStu];
        }
    }
    
    
    [self.manager saveContext];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
